package com.sxkf.core.api.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.jeecg.common.exception.SxkfExceptionEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

/**
 * 统一返回类
 * @author ZhangKeChen
 * @date 2022/8/11
 */
@Data
@Log4j2
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class R<T> {
    @ApiModelProperty(value = "响应码", required = true)
    private Integer code;

    @ApiModelProperty(value = "响应信息", required = true)
    private String message;

    @ApiModelProperty(value = "响应数据")
    private T result;

    @JsonIgnore
    private String onlTable;

    private static final Integer OK_CODE = 200;

    private static final String OK_MSG = "OK";

    /**
     * 三个参数的构造函数（用于返回有数据的接口）
     */
    public R(Integer code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    /**
     * 两个参数的构造函数（用于返回没有数据，但要返回信息的接口）
     */
    public R(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 无参调用两个参数的构造函数（不用每次都编写返回码和返回信息，直接调用该方法）
     */
    public R() {
        this(OK_CODE, OK_MSG);
    }

    /**
     * 返回一个通用的成功方法
     */
    public static <T> R<T> success() {
        return new R<>();
    }

    public static <T> R<T> success(String msg, T result) {
        R<T> response = new R<>();
        response.setMessage(msg);
        response.setResult(result);
        return response;
    }

    /**
     * 返回一个通用的成功方法并且有返回值的
     */
    public static <T> R<T> success(T result) {
        R<T> response = new R<>();
        response.setResult(result);
        return response;
    }

    public static <T> R<T> success(Integer code, T result) {
        R<T> response = new R<>();
        response.setCode(code);
        response.setResult(result);
        return response;
    }

    /**
     * 返回一个通用的失败方法并且有返回值的
     */
    public static <T> R<T> error(Integer code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> error(String msg) {
        return new R<>(500, msg);
    }

    public static <T> R<T> error(SxkfExceptionEnum sxkfExceptionenum) {
        return new R<>(sxkfExceptionenum.getCode(), sxkfExceptionenum.getMsg());
    }

}
